﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OPCPlatformConfigure
{
    public partial class frmMain : Form
    {
        OracleHelper oh;
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString;
            oh = new OracleHelper(connectionString);
            RefreshList(txtConfiName.Text.Trim(), ragOPCType.SelectedIndex);
        }
        public void RefreshList(string name, int opcType)
        {
            string sqlString = string.Format("select * from MDA_MACHINES t where t.MACHINENAME like '%{0}%' and  t.opctype={1} order by t.id desc", name, opcType);
            DataTable dtList = oh.GetTable(sqlString);
            gcList.DataSource = dtList;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (ragOPCType.SelectedIndex == 0)
            {
                frmOPCServerConfi frmServer = new frmOPCServerConfi(null);
                frmServer.ShowDialog();
            }
            else
            {
                frmOPCUaConfi frmUa = new frmOPCUaConfi(gvList.GetFocusedDataRow());
                frmUa.ShowDialog();
            }
            btnSearch_Click(null, null);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gvList.SelectedRowsCount == 0)
            {
                MessageBox.Show("请选择需要编辑的机台信息!");
                return;
            }
            if (ragOPCType.SelectedIndex == 0)
            {
                frmOPCServerConfi frmServer = new frmOPCServerConfi(gvList.GetFocusedDataRow());
                frmServer.ShowDialog();
            }
            else
            {
                frmOPCUaConfi frmUa = new frmOPCUaConfi(gvList.GetFocusedDataRow());
                frmUa.ShowDialog();
            }
            btnSearch_Click(null, null);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            RefreshList(txtConfiName.Text.Trim(), ragOPCType.SelectedIndex);
        }

        private void ragOPCType_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnSearch_Click(null, null);
        }
    }
}
